package com.itboye.eweilisten.util.dbutil.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.util.dbutil.model.UserInfo;

import java.util.ArrayList;

/**
 * Created by Mr.w on 2017/3/1.
 */

public class HistoryAdapter extends BaseAdapter {
    ArrayList<UserInfo> arrayList;
    Context context;
    private View.OnClickListener clickListener;

    public HistoryAdapter(Context context, ArrayList<UserInfo> arrayList) {
        this.arrayList = arrayList;
        this.context = context;
    }

    public void setClickListener(View.OnClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return arrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_history, null);
            viewHolder.delete = (ImageView) view.findViewById(R.id.img_delete);
            viewHolder.txt_user = (TextView) view.findViewById(R.id.txt_username);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.txt_user.setText(arrayList.get(i).getUsername());
        viewHolder.delete.setTag(arrayList.get(i).getId());
        viewHolder.delete.setOnClickListener(clickListener);
        return view;
    }

    public static class ViewHolder {
        TextView txt_user;
        ImageView delete;
    }
}
